package com.sysadmin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.common.base.BaseAction;
import com.common.pager.PageHelper;
import com.common.pager.PagerVO;
import com.common.util.StringUtil;
import com.sysadmin.service.MenuService;
import com.sysadmin.vo.MenusVO;
import com.sysadmin.vo.UsersVO;

public class MenuAction extends BaseAction {
	private MenuService service = new MenuService();
	
	private List<MenusVO> list = new ArrayList<MenusVO>();
	private MenusVO vo = new MenusVO();
	private PagerVO pagerVO = null;
	private String id="0";
	private String pid = "0";

	private Map<String,String> menuMap = new HashMap<String, String>();
	private String roleId="";
	public String getRoleId() {
		return roleId;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	private String ids = null;
	public String find(){
		log.info("==========find============");
		PageHelper pager = new PageHelper(this.getRequest());
		
		list = service.find(pid,vo,pager);
		pagerVO = pager.paginate();
		menuMap = service.findMap();
		return topager("/pages/sys/menu/list.jsp");
		
	}
	public String findByPid(){
		log.info("==========find============"+pid);
		PageHelper pager = new PageHelper(this.getRequest());
		
		if("0".equals(pid)){
			list = service.find(pid,vo,pager);
			pid="-1";
			id = "0";
		}else{
			list = service.find(pid,vo,pager);
			MenusVO menu = service.get(pid);
			pid=menu.getPid();
			id = menu.getId();
			
		}
		pagerVO = pager.paginate();
		menuMap = service.findMap();
		return topager("/pages/sys/menu/list.jsp");
	}
	public String edit(){
		log.info("==========edit============");
		if(!StringUtil.isBlank(id)){
			vo = service.get(id);
		}
		
		return topager("/pages/sys/menu/edit.jsp");
		
	}
	public String add(){
		log.info("==========add============");
		if(!StringUtil.isBlank(pid)){
			vo.setPid(pid);
		}

		return topager("/pages/sys/menu/edit.jsp");
		
	}
	public String save(){
		log.info("==========save============");
		String isSave = service.save(vo);
		
		this.getRequest().setAttribute("saveFlag", isSave);
		return topager("/pages/sys/menu/edit.jsp");
	}
	public void delete(){
		log.info("==========delete============"+ids);
		service.delete(ids);
		
	}
	
	public String tree(){
		HttpServletResponse response = ServletActionContext.getResponse();
		String treeStr = service.getMenuTree(roleId);
		
		log.info("treeStr==========="+treeStr);
		try {
			response.getWriter().write(treeStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	public String maintree(){
		UsersVO users = (UsersVO) this.getSession().getAttribute("users");
		HttpServletResponse response = ServletActionContext.getResponse();
		String treeStr = service.getMenuTree4User(users);
		
		log.info("treeStr==========="+treeStr);
		try {
			response.getWriter().write(treeStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}


	public PagerVO getPagerVO() {
		return pagerVO;
	}

	public void setPagerVO(PagerVO pagerVO) {
		this.pagerVO = pagerVO;
	}

	public String getIds() {
		return ids;
	}



	public void setIds(String ids) {
		this.ids = ids;
	}


	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public List<MenusVO> getList() {
		return list;
	}

	public void setList(List<MenusVO> list) {
		this.list = list;
	}

	public MenusVO getVo() {
		return vo;
	}

	public void setVo(MenusVO vo) {
		this.vo = vo;
	}
	public Map<String, String> getMenuMap() {
		return menuMap;
	}
	public void setMenuMap(Map<String, String> menuMap) {
		this.menuMap = menuMap;
	}
	

	
}
